#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N=2e5+5;
int n,m,q,dfnt,tot,tt;
int a[N],sz[N],ds[N],top[N],prt[N],dfn[N],fp[N],d[N],p[22][N],ans[N],rt[N],s[N];
vector<int>son[N],f[N];
vector<pair<int,int>>Q[N];
void dfs1(int x,int fa){
	d[x]=d[fa]+1,sz[x]=1,prt[x]=fa;
	for(int y:son[x]){
		dfs1(y,x),sz[x]+=sz[y];
		if(!ds[x]||sz[y]>sz[ds[x]])ds[x]=y;
	}
}
void dfs2(int x,int rt){
	top[x]=rt,dfn[x]=++dfnt,fp[dfnt]=x;
	if(ds[x])dfs2(ds[x],rt);
	for(int y:son[x]){
		if(y!=ds[x])dfs2(y,y);
	}
}
inline int ask(int l,int r){
	int k=__lg(r-l+1);
	return p[k][l]|p[k][r-(1<<k)+1];
}
inline int nw(){
	int k=tt?s[tt--]:++tot;
	return f[k].resize(1<<m),k;
}
inline void del(int x){f[x].clear(),s[++tt]=x;}
void slv(int x){
	for(int y:son[x])slv(y);
	rt[x]=nw();
	for(int i=0;i<(1<<m);i++){
		if(i>>a[x]&1)f[rt[x]][i]=1;
		else continue;
		for(int y:son[x])f[rt[x]][i]+=f[rt[y]][i];
	}
	for(auto t:Q[x])ans[t.second]=f[rt[x]][t.first];
	for(int y:son[x])del(rt[y]);
}
signed main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>q>>m;bool tag=1;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=2,f;i<=n;i++)cin>>f,son[f].push_back(i),tag&=f==i-1;
	dfs1(1,0),dfs2(1,1);
	for(int i=1;i<=n;i++)p[0][i]=1<<a[fp[i]];
	for(int i=1;(1<<i)<=n;i++){
		for(int j=1;j+(1<<i)-1<=n;j++)p[i][j]=p[i-1][j]|p[i-1][j+(1<<i-1)];
	}
	if(tag){
		while(q--){
			int s,x,ans=1;cin>>s>>x;
			int l=1,r=x-1,cur=-1;
			while(l<=r){
				int mid=l+r>>1;
				if((ask(mid,x)|s)==s)r=mid-1,cur=mid;
				else l=mid+1;
			}if(~cur)ans+=x-cur;
			l=x+1,r=n,cur=-1;
			while(l<=r){
				int mid=l+r>>1;
				if((ask(x,mid)|s)==s)l=mid+1,cur=mid;
				else r=mid-1;
			}if(~cur)ans+=cur-x;
			cout<<ans<<endl;
		}return 0;
	}
	slv(1);
	for(int i=1;i<=q;i++){
		int s,x,w;cin>>s>>x,w=x;
		while(x){
			if((ask(dfn[top[x]],dfn[x])|s)==s)w=top[x],x=prt[top[x]];
			else{
				int l=1,r=d[x]-d[top[x]]+1,k=-1;
				while(l<=r){
					int mid=l+r>>1;
					if((ask(dfn[x]-mid+1,dfn[x])|s)==s)l=mid+1,k=mid;
					else r=mid-1;
				}if(~k)w=fp[dfn[x]-k+1];
				break;
			}
		}Q[w].push_back({s,i});
	}
	slv(1);
	for(int i=1;i<=q;i++)cout<<max(ans[i],1)<<endl;
	return 0;
}